package tp.pr5;

import tp.pr5.instructions.DefuseInstruction;
import tp.pr5.instructions.DropInstruction;
import tp.pr5.instructions.HelpInstruction;
import tp.pr5.instructions.Instruction;
import tp.pr5.instructions.MoveInstruction;
import tp.pr5.instructions.OperateInstruction;
import tp.pr5.instructions.PickInstruction;
import tp.pr5.instructions.QuitInstruction;
import tp.pr5.instructions.RadarInstruction;
import tp.pr5.instructions.ScanInstruction;
import tp.pr5.instructions.StormInstruction;
import tp.pr5.instructions.TurnInstruction;
import tp.pr5.instructions.exceptions.WrongInstructionFormatException;

/**
 * The interpreter is in charge of converting the user input in an instruction for the robot. Up to now, the valid instructions are:
 *
 * MOVE
 * TURN { LEFT | RIGHT }
 * PICK <ITEM>
 * SCAN [ <ITEM> ]
 * OPERATE <ITEM>
 * RADAR
 * DROP <ITEM>
 * HELP
 * QUIT
 * 
 */

public class Interpreter
{
	/**
	 * Creates all instructions
	 */
	
	private static Instruction[] prototipos = new Instruction[]
	{
		new HelpInstruction(),
		new DropInstruction(),
		new MoveInstruction(),
		new OperateInstruction(),
		new PickInstruction(),
		new QuitInstruction(),
		new RadarInstruction(),
		new ScanInstruction(),
		new TurnInstruction(),
		new StormInstruction(),
		new DefuseInstruction()
	};
	
	/**
	 * Generates a new instruction according to the user input
	 * @param line - A string with the user input
	 * 
	 * @return The instruction read from the given line. If the instruction is not correct, then it returns a not valid instruction
	 * 
	 */
	
	public static Instruction generateInstruction(String line) throws WrongInstructionFormatException
	{
		for (int i = 0; i < prototipos.length; i++) 
		{
			try{
				return prototipos[i].parse(line);
			} catch (Exception e){			
			}
		}
		
		throw new WrongInstructionFormatException();	
	}

	/**
	 * It returns information about all the instructions that the robot understands
	 * 
	 * @return A string with the information about all the available instructions
	 */
	
	public static String interpreterHelp()
	{
		String help = "";
		
		for (int i = 0; i < prototipos.length; i++) 
		{
			help += prototipos[i].getHelp() + System.getProperty("line.separator");

		}
		return "The valid instructions for WALL·E are:" + "\n" + help;

	}
	
}
